Key Features¶
You can try out leafmap by using Goolge Colab or Binder without having to install anything on your computer.
Install leafmap¶
In [1]:
Copied!
import os
import subprocess
import os
import subprocess
In [2]:
Copied!
try:
import leafmap
except ImportError:
print('Installing leafmap ...')
subprocess.check_call(["python", '-m', 'pip', 'install', 'leafmap'])
try:
import leafmap
except ImportError:
print('Installing leafmap ...')
subprocess.check_call(["python", '-m', 'pip', 'install', 'leafmap'])
Use ipyleaflet plotting backend¶
In [3]:
Copied!
import leafmap.foliumap as leafmap
import leafmap.foliumap as leafmap
Use folium plotting backend¶
In [4]:
Copied!
import leafmap
import leafmap
Create an interactive map¶
In [5]:
Copied!
m = leafmap.Map(center=(40, -100), zoom=4)
m
m = leafmap.Map(center=(40, -100), zoom=4)
m
Out[5]:
Make this Notebook Trusted to load map: File -> Trust Notebook
Customize map height¶
In [6]:
Copied!
m = leafmap.Map(height="400px", width="800px")
m
m = leafmap.Map(height="400px", width="800px")
m
Out[6]:
Set control visibility¶
In [7]:
Copied!
m = leafmap.Map(
draw_control=False,
measure_control=False,
fullscreen_control=False,
attribution_control=True,
)
m
m = leafmap.Map(
draw_control=False,
measure_control=False,
fullscreen_control=False,
attribution_control=True,
)
m
Out[7]:
Make this Notebook Trusted to load map: File -> Trust Notebook
Change basemaps¶
In [8]:
Copied!
m = leafmap.Map()
m.add_basemap("Esri.NatGeoWorldMap")
m
m = leafmap.Map()
m.add_basemap("Esri.NatGeoWorldMap")
m
Out[8]:
Make this Notebook Trusted to load map: File -> Trust Notebook
Add XYZ tile layer¶
In [9]:
Copied!
m = leafmap.Map()
m.add_tile_layer(
url="https://mt1.google.com/vt/lyrs=y&x={x}&y={y}&z={z}",
name="Google Satellite",
attribution="Google",
)
m
m = leafmap.Map()
m.add_tile_layer(
url="https://mt1.google.com/vt/lyrs=y&x={x}&y={y}&z={z}",
name="Google Satellite",
attribution="Google",
)
m
Out[9]:
Make this Notebook Trusted to load map: File -> Trust Notebook
Add WMS tile layer¶
In [10]:
Copied!
m = leafmap.Map()
naip_url = 'https://services.nationalmap.gov/arcgis/services/USGSNAIPImagery/ImageServer/WMSServer?'
m.add_wms_layer(
url=naip_url, layers='0', name='NAIP Imagery', format='image/png', shown=True
)
m
m = leafmap.Map()
naip_url = 'https://services.nationalmap.gov/arcgis/services/USGSNAIPImagery/ImageServer/WMSServer?'
m.add_wms_layer(
url=naip_url, layers='0', name='NAIP Imagery', format='image/png', shown=True
)
m
Out[10]:
Make this Notebook Trusted to load map: File -> Trust Notebook
Add COG layer¶
In [11]:
Copied!
m = leafmap.Map()
url = 'https://opendata.digitalglobe.com/events/california-fire-2020/pre-event/2018-02-16/pine-gulch-fire20/1030010076004E00.tif'
m.add_cog_layer(url, name="Fire (pre-event)")
m
m = leafmap.Map()
url = 'https://opendata.digitalglobe.com/events/california-fire-2020/pre-event/2018-02-16/pine-gulch-fire20/1030010076004E00.tif'
m.add_cog_layer(url, name="Fire (pre-event)")
m
Out[11]:
Make this Notebook Trusted to load map: File -> Trust Notebook
Add STAC layer¶
In [12]:
Copied!
m = leafmap.Map()
url = 'https://canada-spot-ortho.s3.amazonaws.com/canada_spot_orthoimages/canada_spot5_orthoimages/S5_2007/S5_11055_6057_20070622/S5_11055_6057_20070622.json'
m.add_stac_layer(url, bands=['B3', 'B2', 'B1'], name='False color')
m
m = leafmap.Map()
url = 'https://canada-spot-ortho.s3.amazonaws.com/canada_spot_orthoimages/canada_spot5_orthoimages/S5_2007/S5_11055_6057_20070622/S5_11055_6057_20070622.json'
m.add_stac_layer(url, bands=['B3', 'B2', 'B1'], name='False color')
m
Out[12]:
Make this Notebook Trusted to load map: File -> Trust Notebook
Add legend¶
In [13]:
Copied!
m = leafmap.Map()
url = "https://www.mrlc.gov/geoserver/mrlc_display/NLCD_2016_Land_Cover_L48/wms?"
m.add_wms_layer(
url,
layers="NLCD_2016_Land_Cover_L48",
name="NLCD 2016 CONUS Land Cover",
format="image/png",
transparent=True,
)
m.add_legend(builtin_legend='NLCD')
m
m = leafmap.Map()
url = "https://www.mrlc.gov/geoserver/mrlc_display/NLCD_2016_Land_Cover_L48/wms?"
m.add_wms_layer(
url,
layers="NLCD_2016_Land_Cover_L48",
name="NLCD 2016 CONUS Land Cover",
format="image/png",
transparent=True,
)
m.add_legend(builtin_legend='NLCD')
m
Out[13]:
Make this Notebook Trusted to load map: File -> Trust Notebook
Add colorbar¶
In [14]:
Copied!
m = leafmap.Map()
m.add_basemap('USGS 3DEP Elevation')
colors = ['006633', 'E5FFCC', '662A00', 'D8D8D8', 'F5F5F5']
vmin = 0
vmax = 4000
m.add_colorbar(colors=colors, vmin=vmin, vmax=vmax)
m
m = leafmap.Map()
m.add_basemap('USGS 3DEP Elevation')
colors = ['006633', 'E5FFCC', '662A00', 'D8D8D8', 'F5F5F5']
vmin = 0
vmax = 4000
m.add_colorbar(colors=colors, vmin=vmin, vmax=vmax)
m
Out[14]:
Make this Notebook Trusted to load map: File -> Trust Notebook
Add GeoJSON¶
In [15]:
Copied!
m = leafmap.Map(center=[0, 0], zoom=2)
in_geojson = 'https://raw.githubusercontent.com/giswqs/leafmap/master/examples/data/cable-geo.geojson'
m.add_geojson(in_geojson, layer_name="Cable lines")
m
m = leafmap.Map(center=[0, 0], zoom=2)
in_geojson = 'https://raw.githubusercontent.com/giswqs/leafmap/master/examples/data/cable-geo.geojson'
m.add_geojson(in_geojson, layer_name="Cable lines")
m
Out[15]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [16]:
Copied!
# Add a GeoJSON with random filled color to the map.
m = leafmap.Map(center=[0, 0], zoom=2)
url = "https://raw.githubusercontent.com/giswqs/leafmap/master/examples/data/countries.geojson"
m.add_geojson(
url, layer_name="Countries", fill_colors=['red', 'yellow', 'green', 'orange']
)
m
# Add a GeoJSON with random filled color to the map.
m = leafmap.Map(center=[0, 0], zoom=2)
url = "https://raw.githubusercontent.com/giswqs/leafmap/master/examples/data/countries.geojson"
m.add_geojson(
url, layer_name="Countries", fill_colors=['red', 'yellow', 'green', 'orange']
)
m
Out[16]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [17]:
Copied!
# Use custom style and hover_style functions.
m = leafmap.Map(center=[0, 0], zoom=2)
url = "https://raw.githubusercontent.com/giswqs/leafmap/master/examples/data/countries.geojson"
style = {
"stroke": True,
"color": "#0000ff",
"weight": 2,
"opacity": 1,
"fill": True,
"fillColor": "#0000ff",
"fillOpacity": 0.1,
}
hover_style = {"fillOpacity": 0.7}
m.add_geojson(url, layer_name="Countries", style=style, hover_style=hover_style)
m
# Use custom style and hover_style functions.
m = leafmap.Map(center=[0, 0], zoom=2)
url = "https://raw.githubusercontent.com/giswqs/leafmap/master/examples/data/countries.geojson"
style = {
"stroke": True,
"color": "#0000ff",
"weight": 2,
"opacity": 1,
"fill": True,
"fillColor": "#0000ff",
"fillOpacity": 0.1,
}
hover_style = {"fillOpacity": 0.7}
m.add_geojson(url, layer_name="Countries", style=style, hover_style=hover_style)
m
Out[17]:
Make this Notebook Trusted to load map: File -> Trust Notebook
Add shapefile¶
In [18]:
Copied!
m = leafmap.Map(center=[0, 0], zoom=2)
in_shp = 'https://github.com/giswqs/leafmap/raw/master/examples/data/countries.zip'
m.add_shp(in_shp, layer_name="Countries")
m
m = leafmap.Map(center=[0, 0], zoom=2)
in_shp = 'https://github.com/giswqs/leafmap/raw/master/examples/data/countries.zip'
m.add_shp(in_shp, layer_name="Countries")
m
Out[18]:
Make this Notebook Trusted to load map: File -> Trust Notebook
Add KML¶
In [19]:
Copied!
try:
import geopandas
except ImportError:
print('Installing geopandas ...')
subprocess.check_call(["python", '-m', 'pip', 'install', 'geopandas'])
try:
import geopandas
except ImportError:
print('Installing geopandas ...')
subprocess.check_call(["python", '-m', 'pip', 'install', 'geopandas'])
In [20]:
Copied!
m = leafmap.Map()
in_kml = 'https://raw.githubusercontent.com/giswqs/leafmap/master/examples/data/us-states.kml'
m.add_kml(in_kml, layer_name="US States KML")
m
m = leafmap.Map()
in_kml = 'https://raw.githubusercontent.com/giswqs/leafmap/master/examples/data/us-states.kml'
m.add_kml(in_kml, layer_name="US States KML")
m
Out[20]:
Make this Notebook Trusted to load map: File -> Trust Notebook
Add GeoDataFrame¶
In [21]:
Copied!
import geopandas as gpd
m = leafmap.Map()
gdf = gpd.read_file(
"https://github.com/giswqs/leafmap/raw/master/examples/data/cable-geo.geojson"
)
m.add_gdf(gdf, layer_name="Cable lines")
m
import geopandas as gpd
m = leafmap.Map()
gdf = gpd.read_file(
"https://github.com/giswqs/leafmap/raw/master/examples/data/cable-geo.geojson"
)
m.add_gdf(gdf, layer_name="Cable lines")
m
Out[21]:
Make this Notebook Trusted to load map: File -> Trust Notebook
Create heat map¶
In [22]:
Copied!
m = leafmap.Map()
in_csv = "https://raw.githubusercontent.com/giswqs/leafmap/master/examples/data/world_cities.csv"
m.add_heatmap(
in_csv,
latitude="latitude",
longitude='longitude',
value="pop_max",
name="Heat map",
radius=20,
)
m = leafmap.Map()
in_csv = "https://raw.githubusercontent.com/giswqs/leafmap/master/examples/data/world_cities.csv"
m.add_heatmap(
in_csv,
latitude="latitude",
longitude='longitude',
value="pop_max",
name="Heat map",
radius=20,
)
In [23]:
Copied!
colors = ['blue', 'lime', 'red']
vmin = 0
vmax = 10000
m.add_colorbar(colors=colors, vmin=vmin, vmax=vmax)
m.add_title("World Population Heat Map", font_size="20px", align="center")
m
colors = ['blue', 'lime', 'red']
vmin = 0
vmax = 10000
m.add_colorbar(colors=colors, vmin=vmin, vmax=vmax)
m.add_title("World Population Heat Map", font_size="20px", align="center")
m
Out[23]:
Make this Notebook Trusted to load map: File -> Trust Notebook
Save map to HTML¶
In [24]:
Copied!
m = leafmap.Map()
m.add_basemap("Esri.NatGeoWorldMap")
m
m = leafmap.Map()
m.add_basemap("Esri.NatGeoWorldMap")
m
Out[24]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [25]:
Copied!
m.to_html("mymap.html")
m.to_html("mymap.html")
Add Planet imagery¶
In [26]:
Copied!
# os.environ["PLANET_API_KEY"] = "12345"
# os.environ["PLANET_API_KEY"] = "12345"
In [27]:
Copied!
m = leafmap.Map()
m.add_planet_by_month(year=2020, month=8)
m.add_planet_by_quarter(year=2019, quarter=2)
m
m = leafmap.Map()
m.add_planet_by_month(year=2020, month=8)
m.add_planet_by_quarter(year=2019, quarter=2)
m
Out[27]:
Make this Notebook Trusted to load map: File -> Trust Notebook
Add OpenStreetMap data¶
Add OSM data of place(s) by name or ID to the map.
In [28]:
Copied!
m = leafmap.Map(toolbar_control=False, layers_control=True)
m.add_osm_from_geocode("New York City", layer_name='NYC')
m
m = leafmap.Map(toolbar_control=False, layers_control=True)
m.add_osm_from_geocode("New York City", layer_name='NYC')
m
Out[28]:
Make this Notebook Trusted to load map: File -> Trust Notebook
Add OSM entities within boundaries of geocodable place(s) to the map.
In [29]:
Copied!
m = leafmap.Map(toolbar_control=False, layers_control=True)
place = "Bunker Hill, Los Angeles, California"
tags = {"building": True}
m.add_osm_from_place(place, tags, layer_name="Los Angeles, CA")
m
m = leafmap.Map(toolbar_control=False, layers_control=True)
place = "Bunker Hill, Los Angeles, California"
tags = {"building": True}
m.add_osm_from_place(place, tags, layer_name="Los Angeles, CA")
m
Out[29]:
Make this Notebook Trusted to load map: File -> Trust Notebook
Use heremap plotting backend¶
- A HERE developer account, free and available under HERE Developer Portal
- An API key from the HERE Developer Portal
- Export API key into environment variable
HEREMAPS_API_KEY
export HEREMAPS_API_KEY=YOUR-ACTUAL-API-KEY
In [30]:
Copied!
import leafmap.heremap as leafmap
import leafmap.heremap as leafmap
Set the API Key.
In [31]:
Copied!
# os.environ["HEREMAPS_API_KEY"] = "12345"
# os.environ["HEREMAPS_API_KEY"] = "12345"
In [32]:
Copied!
api_key = os.environ.get("HEREMAPS_API_KEY") # read api_key from environment variable.
api_key = os.environ.get("HEREMAPS_API_KEY") # read api_key from environment variable.
Create an interactive map
In [33]:
Copied!
m = leafmap.Map(api_key=api_key, center=(40, -100), zoom=4)
m
m = leafmap.Map(api_key=api_key, center=(40, -100), zoom=4)
m
Last update:
2022-02-24